package edu.whut.mall.user.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.whut.mall.common.mybatis.ParamsMap;
import edu.whut.mall.common.vo.Page;
import edu.whut.mall.user.mapper.AddressMapper;
import edu.whut.mall.user.model.Address;
import edu.whut.mall.user.service.IAddressService;
import edu.whut.mall.user.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Dary
 * @projectName mall-refactor
 * @date 2021/6/28 16:28
 */
@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public AddressVo getOneUserAddress(int uaId, int userId) throws Exception {
        ParamsMap map = ParamsMap.getPageInstance();
        map.put("userId", userId);
        map.put("uaId", uaId);
        map.put("isDeleted", 0);
        return addressMapper.getOneUserAddressById(map);
    }

    @Override
    public AddressVo addOneUserAddress(int userId, int areaCode, String address, String realName, String phone)throws Exception{
        ParamsMap map = ParamsMap.getPageInstance();
        map.put("userId",userId);
        map.put("areaCode",areaCode);
        map.put("address",address);
        map.put("realName",realName);
        map.put("phone",phone);
        AddressVo isExist = addressMapper.getOneUserAddressByAll(map);
        if (isExist != null) {
            if (isExist.isDeleted()) {
                addressMapper.restoreOneUserAddress(map);
            } else {
                return null;
            }
        } else {
            Address userAddress = new Address(userId, areaCode, null, null, address, realName, phone);
            addressMapper.insert(userAddress);
        }
        return addressMapper.getOneUserAddressByAll(map);
    }

    @Override
    public AddressVo updateOneUserAddress(int id,int userId, Integer areaCode, String address, String realName, String phone) throws Exception {
        ParamsMap map = ParamsMap.getPageInstance();
        map.put("isDeleted",0);
        map.put("uaId",id);
        map.put("userId",userId);
        Address userAddress = addressMapper.selectById(map);
        if(userAddress == null){
            return null;
        }
        if(areaCode != null){
            userAddress.setAreaCode(areaCode);
        }
        userAddress.setAddress(address);

        if(realName != null){
            userAddress.setRealName(realName);
        }
        if(phone != null){
            userAddress.setPhone(phone);
        }
        userAddress.setUpdateTime(new Date());
        addressMapper.updateOneUserAddressById(userAddress);
//        addressMapper.updateById(userAddress);
        return addressMapper.getOneUserAddressById(map);
    }

    @Override
    public Boolean deleteOneUserAddress(int userId, int uaId)throws Exception{
        ParamsMap map = ParamsMap.getPageInstance();
        map.put("userId",userId);
        map.put("uaId",uaId);
        map.put("isDeleted",0);
        AddressVo isExist = addressMapper.getOneUserAddressById(map);
        if (isExist == null) {
            return false;
        }else{
            addressMapper.deleteOneUserAddressById(map);
        }
        return true;
    }

    @Override
    public Boolean updateOneUserAddressAsDefault(int userId, int uaId)throws Exception{
        ParamsMap map = ParamsMap.getPageInstance();
        map.put("userId",userId);
        map.put("uaId",uaId);
        map.put("isDeleted",0);
        map.put("isDefault",1);
        AddressVo isDefault = addressMapper.getOneUserAddressAsDefaultById(map);
        if(isDefault == null){
        }else {
            addressMapper.updateOneUserAddressFreeDefault(map);
        }
        return addressMapper.updateOneUserAddressAsDefault(map);
    }

    @Override
    public Page<AddressVo> getUserAddressPageList(int current, int size, int userId, String address,
                                                  String orderBy, Boolean asc) {
        ParamsMap map = ParamsMap.getPageInstance(current, size, orderBy, asc);
        if (address != null && !StringUtils.isEmpty(address)) {
            map.put("address", address);
        }
        map.put("userId", userId);
        map.put("isDeleted", 0);
        List<AddressVo> userList = addressMapper.findPageByAddress(map);
        Integer count = addressMapper.findCountByAddress(map);
        return new Page<>(userList, count, current, size);
    }
}

